package cz.apnetis.fitweb.shared;

import java.util.Date;

/** Day food kind
 * @author Lukas "benzin" Benda <a href="mailto:bendal@apnet.cz">bendal@apnet.cz</a>
 * @since Sep 28, 2010
 */
public enum DayFoodKind {

  BREAKFAST("5:00", "9:44"),
  SNACK_MORNING("9:45", "11:29"),
  LUNCH("11:30", "14:14"),
  SNACK_AFTERNOON("14:15", "16:44"),
  DINER("16:45", "18:44"),
  SUPER("18:45", "20:29"),
  PARTY("20:30", "12:00"),
  SUPLEMENT("00:00", "00:00"),
  EXTRA("00:01", "4:59"), ;

  private static class Dimensions {
    public int hourFrom, minuteFrom, hourTo, minuteTo;
  }

  private final Dimensions dimensions;

  private String i18nString;

  private DayFoodKind(final String timeFrom, final String timeTo) {
    this.dimensions = new Dimensions();
    final String[] fa = timeFrom.split(":");
    final String[] ta = timeTo.split(":");

    this.dimensions.hourFrom = Integer.parseInt(fa[0]);
    this.dimensions.minuteFrom = Integer.parseInt(fa[1]);
    this.dimensions.hourTo = Integer.parseInt(ta[0]);
    this.dimensions.minuteTo = Integer.parseInt(ta[1]);
  }

  public final Dimensions getDimensions() {
    return this.dimensions;
  }

  /** Return localized version of this enum in current locale */
  public final String getI18nString() {
    return this.i18nString;
  }

  public final void seI18nString(final String newI18nString) {
    this.i18nString = newI18nString;
  }

  /** Return kind of food for given time */
  public static final DayFoodKind kindForTime(final Date time) {
    final int hour = time.getHours();
    final int minute = time.getMinutes();
    for (final DayFoodKind dfk : DayFoodKind.values()) {
      if (SUPLEMENT != dfk) {
        if (dfk.getDimensions().hourFrom <= hour
            && dfk.getDimensions().minuteFrom <= minute
            && dfk.getDimensions().hourTo >= hour
            && dfk.getDimensions().minuteTo >= minute) {
          return dfk;
        }
      }
    }
    return EXTRA;
  }
}
